/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package at.irian.demo.jsfatwork.view.page;

import at.gp.web.jsf.extval.validation.bypass.annotation.BypassValidation;
import at.irian.demo.jsfatwork.domain.Answer;
import at.irian.demo.jsfatwork.domain.Group;
import at.irian.demo.jsfatwork.domain.Poll;
import at.irian.demo.jsfatwork.service.GroupService;
import at.irian.demo.jsfatwork.service.PollService;
import at.irian.demo.jsfatwork.view.PageBean;
import at.irian.demo.jsfatwork.view.security.UserHolder;
import org.apache.myfaces.extensions.validator.baseval.annotation.Required;
import org.apache.myfaces.orchestra.viewController.annotations.ViewController;

import javax.faces.model.SelectItem;
import javax.inject.Inject;
import javax.inject.Named;
import java.util.ArrayList;
import java.util.List;

/**
 * @author JSF@Work Project Team
 */
@PageBean
@Named("pollPage")
@ViewController(viewIds = "/pages/polls/createPoll.xhtml")
public class PollPage extends AbstractPage
{
    @Inject
    private UserHolder userHolder;

    @Inject
    private PollService pollService;

    @Inject
    private GroupService groupService;

    @Required
    private String selectedGroup;

    private Poll currentPoll;

    private List<Answer> possibleAnswers;

    private List<Group> foundGroups;

    private List<SelectItem> groupList;

    private Answer selectedAnswer;

    @Override
    public void preparePageForRendering()
    {
        if (currentPoll == null)
        {
            this.currentPoll = this.pollService.createNew();
            this.possibleAnswers = new ArrayList<Answer>();
            addNewAnswer();

            this.foundGroups = this.groupService.findAll();

            this.groupList = new ArrayList<SelectItem>();

            String groupName;
            for (Group group : this.foundGroups)
            {
                groupName = group.getName();
                this.groupList.add(new SelectItem(groupName, groupName));
            }
        }
    }

    @BypassValidation(all = true /*bypass skipable and non-skipable constraints*/)
    public void addNewAnswer()
    {
        this.possibleAnswers.add(new Answer());
    }

    @BypassValidation(all = true /*bypass skipable and non-skipable constraints*/)
    public void removeAnswer()
    {
        if (this.possibleAnswers.size() > 1)
        {
            this.possibleAnswers.remove(this.selectedAnswer);
        }
    }

    public String cancel()
    {
        cleanup();
        return "pages/home";
    }

    @BypassValidation(all = true /*bypass skipable and non-skipable constraints*/)
    public void addYesNoAnswers()
    {
        if (isDefaultAnswerList())
        {
            this.possibleAnswers.clear();
        }

        Answer newAnswer = new Answer(getMessageText("answer_yes"));
        this.possibleAnswers.add(newAnswer);
        newAnswer = new Answer(getMessageText("answer_no"));
        this.possibleAnswers.add(newAnswer);
    }

    private boolean isDefaultAnswerList()
    {
        if (this.possibleAnswers.size() > 1)
        {
            return false;
        }
        String answerText = this.possibleAnswers.iterator().next().getDescription();

        return answerText == null || answerText.equals("");
    }

    public void createPoll()
    {
        this.currentPoll.setOwner(this.userHolder.loadUser());
        this.currentPoll.setGroup(findSelectedGroup());

        if (this.pollService.createPoll(this.currentPoll, this.possibleAnswers))
        {
            addMessage("confirmation_poll_submitted");
            cleanup();
        }
        else
        {
            addMessage("violation_invalid_poll");
        }
    }

    private Group findSelectedGroup()
    {
        for (Group group : this.foundGroups)
        {
            if (group.getName().equals(this.selectedGroup))
            {
                return group;
            }
        }
        return null;
    }

    /*
    * generated
    */

    public Poll getCurrentPoll()
    {
        return currentPoll;
    }

    public List<SelectItem> getGroupList()
    {
        return groupList;
    }

    public String getSelectedGroup()
    {
        return selectedGroup;
    }

    public void setSelectedGroup(String selectedGroup)
    {
        this.selectedGroup = selectedGroup;
    }

    public List<Answer> getPossibleAnswers()
    {
        return possibleAnswers;
    }

    public void setSelectedAnswer(Answer selectedAnswer)
    {
        this.selectedAnswer = selectedAnswer;
    }
}